/**
 * Created by wry on 2015/5/14.
 */

var lotteryActivityService = require('../../services/backoffice/backOffice-lotteryActivityService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');

exports.getActivityListForPage = function (req, res) {
    var input = req.body;
    lotteryActivityService.getActivityListForPage(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getActivityListForPage ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                list: result[1],
                totalPage: result[2]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.getProducts = function (req, res) {
    var input = req.body;
    lotteryActivityService.getProducts(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getProducts ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                list: result[0],
                totalPage: result[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};


exports.addProduct = function (req, res) {
    var input = req.body;
    lotteryActivityService.addProduct(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when addProduct ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.deleteProduct = function (req, res) {
    var input = req.body;
    lotteryActivityService.deleteProduct(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteProduct ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.deleteProduct = function (req, res) {
    var input = req.body;
    lotteryActivityService.deleteProduct(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteProduct ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.findProductById = function (req, res) {
    var input = req.body;
    lotteryActivityService.findProductById(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when findProductById ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.updateProductInventory = function (req, res) {
    var input = req.body;
    lotteryActivityService.updateProductInventory(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when updateProductInventory ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getCommunitysByLotteryId = function (req, res) {
    var input = req.body;
    lotteryActivityService.getCommunitysByLotteryId(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getCommunitysByLotteryId ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                list: result[0],
                totalPage: result[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.deleteLotteryCommunityMap = function (req, res) {
    var input = req.body;
    lotteryActivityService.deleteLotteryCommunityMap(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteLotteryCommunityMap ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getPrizeByLotteryId = function (req, res) {
    var input = req.body;
    lotteryActivityService.getPrizeByLotteryId(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getPrizeByLotteryId ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                list: result[0],
                totalPage: result[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.deletePrize = function (req, res) {
    var input = req.body;
    lotteryActivityService.deletePrize(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteLotteryCommunityMap ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getAllProduct = function (req, res) {
    var input = req.body;
    lotteryActivityService.getAllProduct(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteLotteryCommunityMap ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.addPrize = function (req, res) {
    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('addPrize - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '奖品创建失败'));
        } else {
            var input = req.body;
            input.prizePic = images[0];
            lotteryActivityService.addPrize(input, function (err, result) {
                if (err) {
                    logger.error('Exception happened when addPrize ...!' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, err.message));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, result));
                }
            })
        }
    });
};


exports.getLotteryPrizeById = function (req, res) {
    var input = req.body;
    lotteryActivityService.getLotteryPrizeById(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLotteryPrizeById ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.updateLotteryPrize = function (req, res) {
    var input = req.body;

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('updateLotteryPrize - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '奖品创建失败'));
        } else {
            input.prizePic = images[0];
            lotteryActivityService.updateLotteryPrize(input, function (err, result) {
                if (err) {
                    logger.error('Exception happened when updateLotteryPrize ...!' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, err.message));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, result));
                }
            })
        }
    })
}

exports.getLotteryAward = function (req, res) {
    var input = req.body;
    lotteryActivityService.getLotteryAward(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getPrizeByLotteryId ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                list: result[0],
                totalPage: result[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.addLottery = function (req, res) {
    var input = req.body;
    lotteryActivityService.addLottery(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when addLottery ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.deleteLottery = function (req, res) {
    var input = req.body;
    lotteryActivityService.deleteLottery(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteLottery ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getLotteryById = function (req, res) {
    var input = req.body;
    lotteryActivityService.getLotteryById(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLotteryById ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.updateLottery = function (req, res) {
    var input = req.body;
    lotteryActivityService.updateLottery(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when updateLottery ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};